//
//  XGStatusDetailHeaderView.swift
//  TodayNews
//
//  Created by monkey on 2020/3/13.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher

class XGStatusDetailHeaderView: UIView
{
    init(statusViewModel: XGStatusViewModel)
    {
        self.statusViewModel = statusViewModel
        super.init(frame: CGRect(x: 0, y: 0, width: TodayNews_Screen_Width, height: statusViewModel.headerViewHeight))
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有属性
    
    private let statusViewModel: XGStatusViewModel
    /// 用户头像
    private lazy var userIconImageView = UIImageView()
    /// 用户名
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 15, textAlignment: .left)
    /// 发布时间
    private lazy var createTimeLabel = UILabel(text: "测试", textColor: UIColor.darkGray, fontSize: 13, textAlignment: .left)
    /// 文字内容
    private lazy var contentLabel: XGRichTextLabel = {
        let label = XGRichTextLabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = TodayNews_Screen_Width - 20.0
        return label
    }()
}

// MARK: - 私有扩展

private extension XGStatusDetailHeaderView
{
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        addSubview(userIconImageView)
        addSubview(userNameLabel)
        addSubview(createTimeLabel)
        addSubview(contentLabel)

        userIconImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        userNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(userIconImageView)
            make.left.equalTo(userIconImageView.snp.right).offset(10)
        }
        createTimeLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(userIconImageView)
            make.left.equalTo(userNameLabel.snp.right).offset(10)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIconImageView.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        switch statusViewModel.itemType {
        case .postContent:
            addPicturesView()
        case .commentOrQuoteContent, .commentOrQuoteOthers:
            addOriginalStatusView()
        default:
            break
        }
        
        if let userIcon = statusViewModel.userIcon {
            userIconImageView.kf.setImage(with: URL(string: userIcon), options: [.processor(RoundCornerImageProcessor(cornerRadius: 20,targetSize: CGSize(width: 40, height: 40)))])
        }
        userNameLabel.text = statusViewModel.userName
        createTimeLabel.text = statusViewModel.createTime
        contentLabel.attributedText = statusViewModel.contentAttributedString
    }
    
    private func addPicturesView() -> Void
    {
        // 没有图片直接返回
        guard let thumbImageList = statusViewModel.thumbImageList, thumbImageList.count > 0 else {
            return
        }
        
        // 有图片 添加配图视图
        let picturesView = XGStatusPicturesView()
        picturesView.backgroundColor = backgroundColor
        addSubview(picturesView)
        picturesView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(statusViewModel.picturesViewSize.height)
        }
        
        picturesView.thumbImageList = thumbImageList
        picturesView.largeImagelist = statusViewModel.largeImagelist
    }
    
    private func addOriginalStatusView() -> Void
    {
        let originalUserView = XGOriginalUserView()
        let referenceContentLabel = XGRichTextLabel()
        referenceContentLabel.numberOfLines = 0
        referenceContentLabel.preferredMaxLayoutWidth = TodayNews_Screen_Width - 20.0
        
        let referenceView = UIView()
        referenceView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        originalUserView.backgroundColor = referenceView.backgroundColor
        addSubview(referenceView)
        referenceView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
        }
        referenceView.addSubview(originalUserView)
        referenceView.addSubview(referenceContentLabel)
        originalUserView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.right.equalToSuperview()
            make.height.equalTo(30)
        }
        referenceContentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(originalUserView.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
        }
        
        if let thumbImageList = statusViewModel.thumbImageList, thumbImageList.count > 0 {
            let picturesView = XGStatusPicturesView()
            picturesView.backgroundColor = referenceView.backgroundColor
            referenceView.addSubview(picturesView)
            picturesView.snp.makeConstraints { (make) in
                make.top.equalTo(referenceContentLabel.snp.bottom)
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(statusViewModel.picturesViewSize.height)
            }
            picturesView.thumbImageList = statusViewModel.thumbImageList
        } else {
            referenceContentLabel.snp.makeConstraints { (make) in
                make.bottom.equalToSuperview()
            }
        }
        
        referenceContentLabel.attributedText = statusViewModel.referenceContentAttributedString
        originalUserView.originalUserInfo = statusViewModel.originalUserInfo
    }
}
